package qe.spider.vo;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.NumberUtil;
import com.google.gson.annotations.Expose;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import qe.spider.grab.ParseThsPlate;
import qe.spider.grab.ParseThsStock;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Data
@Slf4j
public class Plates {
    private String Max;
    private String Min;
    private Integer Count;
    private BigDecimal Time;

    private List<String> Day;
    private List<List<String>> list;

    private Date grabTime;
    private List<Plate> plates = new ArrayList<>();

    public void setTime(BigDecimal time) {

    }

    public List<Plate> refreshPlates(String catetory) {
        if (Time != null) {
            Time = Time.compareTo(new BigDecimal(10000000000L)) < 0 ? Time.multiply(new BigDecimal(1000)) : Time;
            if (grabTime == null || grabTime.before(new Date(Time.longValue()))) grabTime = new Date(Time.longValue());
        } else {
            grabTime = grabTime == null ? new Date() : grabTime;
        }
        Date t = grabTime;
        if (null != plates && !plates.isEmpty()) return plates;
        List<Plate> res = new ArrayList<>();
        if (null == list) return res;
        if (list.isEmpty()) return res;
        for (int i = 0; i < list.size(); i++) {
            List<String> data = list.get(i);
            if (data.size() == 13) {
                Plate plate = new Plate();
                plate.setCode((String) data.get(0));
                plate.setName((String) data.get(1));
                plate.setV1(new BigDecimal(data.get(2)));
                plate.setZhangFu(new BigDecimal(data.get(3)));
                plate.setZhangSu(new BigDecimal(data.get(4)));
                plate.setV4(new BigDecimal(data.get(5)));
                plate.setZhuLiJingE(new BigDecimal(data.get(6)));
                plate.setZhuLiBuy(new BigDecimal(data.get(7)));
                plate.setZhuLiSell(new BigDecimal(data.get(8)));
                plate.setV8(new BigDecimal(data.get(9)));
                plate.setZongLiuTongZhi(new BigDecimal(data.get(10)));
                plate.setQuJianZhangFu(new BigDecimal(data.get(11)));
                plate.setBanKuaiDianShu(data.get(12));
                plate.setGrabTime(t);
                plate.setCategory(catetory);
                res.add(plate);
            }
        }
        return plates = res;
    }

    public List<File> latestTHFFile(String day) {
        Comparator<File> fileTimeDescComparator = (f1, f2) -> {
            Date d1 = f1.getName().contains("_") ? DateUtil.parse(day + " " + f1.getName().split("\\.")[0], "yyyy-MM-dd HHmmss_SSS") : DateUtil.parse(day + " " + f1.getName().split("\\.")[0], "yyyy-MM-dd HHmmss");
            Date d2 = f2.getName().contains("_") ? DateUtil.parse(day + " " + f2.getName().split("\\.")[0], "yyyy-MM-dd HHmmss_SSS") : DateUtil.parse(day + " " + f2.getName().split("\\.")[0], "yyyy-MM-dd HHmmss");
            return d2.compareTo(d1);
        };
        List<File> allFile = ParseThsPlate.disks.stream().map(path ->
                        FileUtil.loopFiles(path + "\\" + day, File::isFile)
                ).flatMap(Collection::stream)
                .filter(f -> f.getName().endsWith(".xls") && f.length() > 10000)
                .filter(f -> (f.getName().contains("_") ? DateUtil.parse(day + " " + f.getName().split("\\.")[0], "yyyy-MM-dd HHmmss_SSS") : DateUtil.parse(day + " " + f.getName().split("\\.")[0], "yyyy-MM-dd HHmmss")).isBefore(grabTime))
                .sorted(fileTimeDescComparator)
                .collect(Collectors.toList());
        return allFile;
    }

    @Expose
    private transient ParseThsPlate parser;

    public List<Plate> parseThs() throws IOException {
        String day = DateUtil.format(grabTime, "yyyy-MM-dd");
        List<File> allFile = latestTHFFile(day);
        if (allFile.isEmpty()) {
            log.error("未抓取到同花顺板块数据");
            return new ArrayList<>();
        }
        File latestFile = allFile.get(0);
        long diff = DateUtil.between(grabTime, latestFile.getName().contains("_") ? DateUtil.parse(day + " " + latestFile.getName().split("\\.")[0], "yyyy-MM-dd HHmmss_SSS") : DateUtil.parse(day + " " + latestFile.getName().split("\\.")[0], "yyyy-MM-dd HHmmss"), DateUnit.MINUTE);
        log.info("抓取到的同花顺板块数据文件为：{}，据抓取时间{}有{}分钟了", latestFile.getAbsolutePath(), DateUtil.format(grabTime, "HHmmss"), diff);
        if (diff > 1) {
            log.error("同花顺最新的板块数据超过1分钟，过期了----最新的文件为：{}", latestFile.getAbsolutePath());
            return new ArrayList<>();
        }
        parser = new ParseThsPlate(latestFile);
        parser.parseAllLines();
        return plates = parser.getAll();
    }
}
